/*******************************************************************************
 * Copyright (c) 2008 Sylvain Sicard.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Sylvain Sicard - initial API and implementation
 ******************************************************************************/
package fr.jade.fraclite.orb;

import java.io.IOException;
import java.lang.reflect.Proxy;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.objectweb.naming.Binder;
import org.objectweb.naming.Name;

public class StubFactory {
  
  ClassLoader cl = null;
  
  Skeleton s = null;
  ExecutorService es;
  
  public StubFactory(ClassLoader cl) {
    this.cl=cl;
    es = Executors.newCachedThreadPool();
  }

  public Object newStub(Binder binder, Class<?>[] itfs, Name name, String signature){
    Stub stub = new Stub(name, binder, signature);
    return Proxy.newProxyInstance(cl, itfs, stub);
  }
  
  public Object newSkeleton(Map hints, Object target, Binder naming) throws IOException{
    int port = 0;
    if (hints != null){
      port = Integer.parseInt((String)hints.get("port"));
    }
    if(s == null){
      s = new Skeleton(es, naming, port);
      s.start();
    }
    s.registerTarget(target);
    return s;
  }
}
